

import { Node, Vec2,  Sprite, SpriteFrame } from 'cc';
import { tween } from 'cc';
import { Vec3 } from 'cc';
import { _decorator, Component } from 'cc';
import UtilTool from '../../tools/UtilTool';
const { ccclass, property } = _decorator;

/* 得分粒子脚本 */
@ccclass('ScoreParticle')
export class ScoreParticle extends Component {
    @property(Sprite)
    sprArr = []

    @property(SpriteFrame)
    sprFrame = []

    private speed = 2000 // 速度
    onLoad(): void {
        
    }

    start() {
        
    }

    /**
     * 初始化
     * @param startPos 开始位置
     * @param endPos 结束位置
     * @param type 类型 [1]粒子 [2]金币
     * @param func 回调
     */
    init(startPos,endPos,type=1,func=null) {
        this.setSprFrame(type)
        let count = Math.floor(this.getRandom(10, 15))
        let oneTime = 0
        for (let i = 0; i < count; i++) {
            if (i>14) break

            setTimeout(() => {
                /* var sprNode = new Node();
                var spr_new = sprNode.addComponent(Sprite)
                spr_new.spriteFrame = this.sprFarame
                sprNode.setPosition(0,0,0)
                this.node.addChild(sprNode) */
                let sprNode = this.sprArr[i].node
                sprNode.active = true
                // 随机大小
                if (type==1) {
                    let size = this.getRandom(2,5)
                    sprNode.setScale(size,size,1)
                }
                else if(type==2) {
                    let size = this.getRandom(0.3,1)
                    sprNode.setScale(size,size,1)
                }
                

                // 随机开始位置
                let disx = this.getRandom(-40, 40)
                let disy = this.getRandom(-40, 40)
                let _startPos = new Vec2(startPos.x + disx, startPos.y + disy)
                sprNode.setPosition(_startPos.x, _startPos.y, 0)

                let disance = UtilTool.getDistance(_startPos, endPos)
                let time = disance / this.speed
                // 飘动画
                tween()
                    .target(sprNode)
                    .delay(0.1)
                    .to(time, { position: new Vec3(endPos.x, endPos.y, 0) })
                    .call(() => {
                        sprNode.active = false

                        if (i==count-1) {
                            if (this.node) {
                                this.node.destroy();
                            }

                            func && func()
                        }
                    })
                    .start()
            }, oneTime);

            oneTime = oneTime + 30
        }
    }

    /**设置粒子贴图 */
    setSprFrame(type) {
        let frame = this.sprFrame[type-1]
        if (type==2 && frame) {
            for (let index = 0; index < this.sprArr.length; index++) {
                const element = this.sprArr[index];
                element.spriteFrame = frame
            }
        }
    }

    /**  
     * 获取范围随机数
     * @param startNumber 随机数开始下标
     * @param endNumber 随机数结束下标
    */
    getRandom(startNumber=0.5, endNumber=1.5) {
        var choice = endNumber - startNumber
        return Math.random() * choice + startNumber
    }
}

